package com.ssd.controller;


import com.ssd.emq.utils.BCrypt;
import com.ssd.entity.Partner;
import com.ssd.entity.dto.PartnerReq;
import com.ssd.entity.dto.PartnerUpdatePwdReq;
import com.ssd.service.IPartnerService;
import com.ssd.view.BaseResponse;
import com.ssd.viewmodel.Pager;
import com.ssd.viewmodel.PartnerViewModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 合作商表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-11-14
 */
@RestController
@RequestMapping("/partner")
@Api(tags = "合作商表 前端控制器")
public class PartnerController {
    @Resource
    private IPartnerService partnerService;

    /**
     * 新增合作商
     * @param partner 合作商数据
     * @return
     */
    @ApiOperation(value = "新增合作商")
    @PostMapping
    public BaseResponse<Boolean> add( @ApiParam(value = "合作商数据") @RequestBody Partner partner){
        // 默认密码为123456，使用BCrypt加密
        partner.setPassword(BCrypt.hashpw("123456",BCrypt.gensalt()));

        return new BaseResponse<>(partnerService.save(partner));
    }

    /**
     * 获取合作商名称
     * @param id 合作商ID
     * @return 合作商名称
     */
    @GetMapping("/name")
    @ApiOperation(value = "获取合作商名称")
    public BaseResponse<String> getPartnerName(@ApiParam(value = "合作商ID") @PathVariable Integer id){
        return new BaseResponse<>(partnerService.getById(id).getName());
    }

    /**
     * 修改合作商信息
     * @param id 合作商ID
     * @param partner 请求数据
     * @return
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "修改合作商信息")
    public BaseResponse<Boolean> update(
        @ApiParam(value = "合作商id") @PathVariable("id") Long id,
        @ApiParam(value = "合作商更新数据") @RequestBody PartnerReq partner
    ){
        return partnerService.modify(id,partner);
    }

     /**
     * 删除合作商
     * @param id 合作商ID
     * @return 删除是否成功
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除合作商")
    public BaseResponse<Boolean> delete(@ApiParam(value = "合作商ID") @PathVariable Integer id){
        return partnerService.delete(id);
    }

    /**
     * 根据合作商Id获取详情
     * @param id 合作商ID
     * @return 合作商详情
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据合作商Id获取详情")
    public BaseResponse<PartnerViewModel> getById(@ApiParam(value = "合作商ID") @PathVariable Integer id){
        //1.根据id 获取合作商实体
        Partner partner = partnerService.getById(id);
        if(partner == null)
            return null;
        PartnerViewModel vo = new PartnerViewModel();
        BeanUtils.copyProperties(partner,vo);

        // 2.远程调用获取售货机数量 TODO
//        vo.setVmCount(vmService.getVmCountByOwnerId(id));
        return new BaseResponse<>(vo);
    }

    /**
     * 查询合作商
     * @param pageIndex 页码
     * @param pageSize 每页大小
     * @param name 合作商名称
     * @return 合作商列表
     */
    @GetMapping("/search")
    @ApiOperation(value = "查询合作商")
    public BaseResponse<Pager<Partner>> search(
            @ApiParam(value = "页码") @RequestParam(value = "pageIndex",required = false,defaultValue = "1") Long pageIndex,
            @ApiParam(value = "每页大小") @RequestParam(value = "pageSize",required = false,defaultValue = "1") Long pageSize,
            @ApiParam(value = "合作商名称") @RequestParam(value = "name",required = false,defaultValue = "") String name){
        return partnerService.search(pageIndex,pageSize,name);
    }

    /**
     * 重置密码
     * @param id 合作商ID
     */
    @PutMapping("/resetPwd/{id}")
    @ApiOperation(value = "重置密码")
    public BaseResponse resetPwd(@ApiParam(value = "合作商ID") @PathVariable Integer id){
       return partnerService.resetPwd(id);
    }

    /**
     * 更新密码
     * @param id 合作商ID
     * @param req 更新密码的请求数据
     * @return 更新是否成功
     */
    @PutMapping("/updatePwd/{id}")
    @ApiOperation(value = "更新密码")
    public BaseResponse modifyPwd(@ApiParam(value = "合作商ID") @PathVariable Integer id,
                             @ApiParam(value = "更新密码的请求数据") @RequestBody PartnerUpdatePwdReq req){
        return partnerService.updatePwd(id,req);
    }
}
